package com.leetcode.q7;

/**
 * <p> description: desc</p>
 *
 * @author tanzc
 * @date 2022/3/29
 **/
public class Reverse {
    static int max_int = 2147483647;
    static int min_int = -2147483648;

    public int reverse(int x) {
        int reverseNum = 0;
        if (x > 0) {
            while (x != 0) {
                if ((max_int - x % 10) / 10 - reverseNum >= 0) {
                    reverseNum = reverseNum * 10 + x % 10;
                    x = x / 10;
                } else {
                    return 0;
                }
            }
        } else {
            while (x != 0) {
                if ((min_int - x % 10) / 10 - reverseNum <= 0) {
                    reverseNum = reverseNum * 10 + x % 10;
                    x = x / 10;
                } else {
                    return 0;
                }
            }
        }

        return reverseNum;
    }
}
